﻿using Unity.Entities;
using Unity.Transforms;
using Unity.Jobs;

namespace Jinndev.Tile2D {

    /// <summary>
    /// 通过坐标是否改变，来控制sequenceFrame.play的值
    /// </summary>
    [UpdateBefore(typeof(SequenceFrameSystem))]
    public class SequencePlayByPositionSystem : JobComponentSystem {

        protected override JobHandle OnUpdate(JobHandle inputDeps) {
            inputDeps = Entities.WithChangeFilter<Translation>().ForEach((ref SequenceFrame sequenceFrame, ref SequencePlayByPosition sequencePlayByPosition, in Translation translation) => {

                sequenceFrame.play = sequencePlayByPosition.alwaysPlay || !translation.Value.xy.Equals(sequencePlayByPosition.lastPosition.xy);
                sequencePlayByPosition.lastPosition = translation.Value;

            }).Schedule(inputDeps);
            inputDeps.Complete();
            return inputDeps;
        }

    }

}
